﻿Public Class fCierreCaja 

    Private Sub fCierreCaja_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        VcierrecajaTableAdapter.Fill(DbFireAndIceDataSet.vcierrecaja)
        TipospagoTableAdapter.Fill(DbFireAndIceDataSet.tipospago)
        modificar.Enabled = True
        Mostrar()
    End Sub

    Private Sub guardar_Click(sender As System.Object, e As System.EventArgs) Handles guardar.Click
        Try
            If IsDBNull(EfectivorealTextEdit1.EditValue) Then
                EfectivorealTextEdit1.EditValue = 0
            End If
            If EfectivorealTextEdit1.EditValue > 0 And EfectivorealTextEdit1.Text <> "" Then
                CierrecajaTableAdapter.UpdateQueryEfectivoReal(EfectivorealTextEdit1.EditValue, IdcierrecajaTextEdit1.EditValue)
                ErrorProvider1.SetError(EfectivorealTextEdit1, Nothing)
                modificar.Enabled = True
                Mostrar()
                AlertControl1.AppearanceCaption.ForeColor = Color.Green
                AlertControl1.Show(Me, "Notificación", "Se actualizó el efectivo real en caja.")
            Else
                ErrorProvider1.SetError(EfectivorealTextEdit1, "Debe ingresar un monto valido")
            End If
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "No se guardó el registro.")
            LogSGS.RegistrarLogSinEnviarCorreo(My.Resources.StringGlobal.prgAngely, "Sentencia para actualizar el efectivo real en caja en pantalla de Cierres de Caja", ex, "Cierre Caja", Name, sender.GetType.FullName.ToString)
        End Try
    End Sub

    Private Sub modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles modificar.Click
        If IdcierrecajaTextEdit1.Text <> "" Then
            EfectivorealTextEdit1.Focus()
            modificar.Enabled = False
            Mostrar()
        End If
    End Sub

    Private Sub cancelar_Click(sender As System.Object, e As System.EventArgs) Handles cancelar.Click
        Try
            EfectivorealTextEdit1.Properties.ReadOnly = False
            tEfectivoFacturado.EditValue = ConexionSGS.EjecutarQueryScalar(String.Format("select cierrecajadetalle.monto from cierrecaja inner join cierrecajadetalle on cierrecaja.idcierrecaja = cierrecajadetalle.idcierrecaja where cierrecajadetalle.idtipopago = 1 AND cierrecaja.idcierrecaja = {0};", Str(IdcierrecajaTextEdit1.EditValue)))
            ErrorProvider1.SetError(EfectivorealTextEdit1, Nothing)
            modificar.Enabled = True
            Mostrar()
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "No se pudo cancelar el proceso.")
            LogSGS.RegistrarLogSinEnviarCorreo(My.Resources.StringGlobal.prgAngely, "Sentencia para cancelar en pantalla de Cierres de Caja", ex, "Cierre Caja", Name, sender.GetType.FullName.ToString)
        End Try
    End Sub

    Private Sub imprimir_Click(sender As System.Object, e As System.EventArgs) Handles imprimir.Click
        Try
            If IdcierrecajaTextEdit1.EditValue = Nothing Then
                MsgBox("Lo datos del cierre no se cargaron completamente, no puede reimprimir este cierre de caja", MsgBoxStyle.Information, "Notificación")
            Else
                Dim report As New rptCierre
                report.VreportecierreTableAdapter.FillById(report.DbFireAndIceDataSet1.vreportecierre, IdcierrecajaTextEdit1.EditValue)
                Dim totalespago As Integer = report.VreportecierreTableAdapter.ScalarQueryObtieneTotalTiposPago(IdcierrecajaTextEdit1.EditValue)
                report.PageHeight = report.PageHeight + report.Bands(DevExpress.XtraReports.UI.BandKind.Detail).HeightF * (totalespago - 1)
                report.Print()
                'report.ShowPreview()

                Dim reportmov As New rptMovimientos
                If reportmov.MovimientoscajaTableAdapter.FillByRangoCierreCaja(reportmov.DbFireAndIceDataSet1.movimientoscaja, NumticketinicialTextEdit.EditValue, NumticketfinalTextEdit.EditValue, NombrepcTextEdit.Text) Then
                    reportmov.VreportemovimientosTableAdapter1.FillByCaja(reportmov.DbFireAndIceDataSet1.vreportemovimientos, NumticketinicialTextEdit.EditValue, NumticketfinalTextEdit.EditValue, NombrepcTextEdit.Text)
                    reportmov.MovimientoscajaTableAdapter.FillByRangoCierreCaja(reportmov.DbFireAndIceDataSet1.movimientoscaja, NumticketinicialTextEdit.EditValue, NumticketfinalTextEdit.EditValue, NombrepcTextEdit.Text)
                    'reportmov.ShowPreview()
                    reportmov.Print()
                Else
                    Dim reportevacio As New rptMovimientosVACIO
                    'reportevacio.ShowPreview()
                    reportevacio.Print()
                End If
            End If
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "Ha ocurrrido un error al intentar imprimir.")
            LogSGS.RegistrarLogSinEnviarCorreo(My.Resources.StringGlobal.prgAngely, "Llamada a los reportes de cierre de caja y movimientos de caja en fCierreCaja", ex, "Cierre Caja", Name, sender.GetType.FullName.ToString)
        End Try
    End Sub

    Private Sub tEfectivoFacturado_EditValueChanged(sender As System.Object, e As System.EventArgs) Handles tEfectivoFacturado.EditValueChanged
        Try
            tBalance.EditValue = tEfectivoFacturado.EditValue
            tBalance.ForeColor = Color.Red
            If Not IsDBNull(EfectivorealTextEdit1.EditValue) Then
                tBalance.EditValue = EfectivorealTextEdit1.EditValue - tEfectivoFacturado.EditValue
                If tBalance.EditValue = 0 Then
                    tBalance.ForeColor = Color.White
                    tBalance.BackColor = Color.Green
                Else
                    tBalance.ForeColor = Color.White
                    tBalance.BackColor = Color.Red
                End If
            End If
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "No se cargó el balance del cierre.")
            LogSGS.RegistrarLogSinEnviarCorreo(My.Resources.StringGlobal.prgAngely, "Cargando tBalance igual a tEfectivoFacturado en pantalla fCierreCaja.", ex, "Cierre Caja", Name, sender.GetType.FullName.ToString)
        End Try
    End Sub

    Private Sub SimpleButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        My.Forms.frmBuscarCierreCaja.ShowDialog()
    End Sub

    Private Sub Mostrar()
        If modificar.Enabled = True Then
            guardar.Enabled = False
            cancelar.Enabled = False
            modificar.Enabled = True
            SimpleButton1.Enabled = True
            EfectivorealTextEdit1.Properties.ReadOnly = True
        Else
            SimpleButton1.Enabled = False
            guardar.Enabled = True
            cancelar.Enabled = True
            modificar.Enabled = False
            EfectivorealTextEdit1.Properties.ReadOnly = False
        End If
    End Sub

    Private Sub IdcierrecajaTextEdit1_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdcierrecajaTextEdit1.EditValueChanged
        If Not IsDBNull(IdcierrecajaTextEdit1.EditValue) Then
            Try
                If IdcierrecajaTextEdit1.Text <> "" Then
                    VcierrecajaTableAdapter.FillByIdFact(DbFireAndIceDataSet.vcierrecaja, IdcierrecajaTextEdit1.EditValue)
                    CierrecajadetalleTableAdapter.FillByID(DbFireAndIceDataSet.cierrecajadetalle, IdcierrecajaTextEdit1.EditValue)
                    tEfectivoFacturado.EditValue = ConexionSGS.EjecutarQueryScalar(String.Format("select cierrecajadetalle.monto from cierrecaja inner join cierrecajadetalle on cierrecaja.idcierrecaja = cierrecajadetalle.idcierrecaja where cierrecajadetalle.idtipopago = 1 AND cierrecaja.idcierrecaja = {0};", Str(IdcierrecajaTextEdit1.EditValue)))
                    modificar.Enabled = True
                    Mostrar()
                    If IsDBNull(IdcierrecajaTextEdit1.EditValue) Then
                        IdcierrecajaTextEdit1.EditValue = 0
                    End If
                    If IsDBNull(EfectivorealTextEdit1.EditValue) Then
                        EfectivorealTextEdit1.EditValue = 0
                    End If
                End If
            Catch ex As Exception
                AlertControl1.AppearanceCaption.ForeColor = Color.Red
                AlertControl1.Show(Me, "Error", "No se calcularon algunos datos de inicio.")
                LogSGS.RegistrarLogSinEnviarCorreo(My.Resources.StringGlobal.prgAngely, "Sentencia para filtrar detalle de cierre de caja por idcierre.", ex, "Cierre Caja", Name, sender.GetType.FullName.ToString)
            End Try

        End If
    End Sub

    Private Sub EfectivorealTextEdit1_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EfectivorealTextEdit1.EditValueChanged
        Try
            tBalance.EditValue = tEfectivoFacturado.EditValue
            tBalance.ForeColor = Color.Red
            If Not IsDBNull(EfectivorealTextEdit1.EditValue) Then
                tBalance.EditValue = EfectivorealTextEdit1.EditValue - tEfectivoFacturado.EditValue
                If tBalance.EditValue = 0 Then
                    tBalance.ForeColor = Color.White
                    tBalance.BackColor = Color.Green
                Else
                    tBalance.ForeColor = Color.White
                    tBalance.BackColor = Color.Red
                End If
            End If
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "No se cargó el balance del cierre.")
            LogSGS.RegistrarLogSinEnviarCorreo(My.Resources.StringGlobal.prgAngely, "Cargando tBalance igual a tEfectivoFacturado en pantalla fCierreCaja.", ex, "Cierre Caja", Name, sender.GetType.FullName.ToString)
        End Try
    End Sub
End Class